﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace MslDiary.Control
{
    /// <summary>
    /// PopUP.xaml 的交互逻辑
    /// </summary>
    public partial class PopUP
    {
        private readonly double _screenWidth;
        private readonly DispatcherTimer _hideTimer;
        private readonly DispatcherTimer _closeTimer;
        private readonly Storyboard _sboardShow;
        private readonly Storyboard _sboardHide;

        /// <summary> 消息主题 </summary>
        public string Subject { get; set; }
        /// <summary>  消息内容 </summary>
        public string Msg { get; set; }
        /// <summary> 窗口标题 </summary>
        public string PopTitle { get; set; }
        public PopUP()
        {
            InitializeComponent();
            this.Closed += PopUP_Closed;
            this.Loaded += PopWin_Loaded;

            this._closeTimer = new DispatcherTimer();
            this._hideTimer = new DispatcherTimer();

            this._closeTimer = new DispatcherTimer();
            this._closeTimer.Interval = TimeSpan.FromSeconds(3);
            this._closeTimer.Tick += CloseTimer_Tick;

            this._hideTimer = new DispatcherTimer();
            this._hideTimer.Interval = TimeSpan.FromSeconds(10);
            this._hideTimer.Tick += HideTimer_Tick;
            this._hideTimer.Start();

            this._screenWidth = SystemParameters.WorkArea.Width;
            this.Left = this._screenWidth;
            this.Top = SystemParameters.WorkArea.Height - this.Height;

            this._sboardShow = new Storyboard();
            DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames();
            EasingDoubleKeyFrame keyFrame = new EasingDoubleKeyFrame(this._screenWidth - this.Width, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)));

            animation.KeyFrames.Add(keyFrame);
            this._sboardShow.Children.Add(animation);
            Storyboard.SetTarget(animation, this);
            Storyboard.SetTargetProperty(animation, new PropertyPath("(Canvas.Left)"));

            this._sboardHide = new Storyboard();
            animation = new DoubleAnimationUsingKeyFrames();
            keyFrame = new EasingDoubleKeyFrame(this._screenWidth, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2)));

            animation.KeyFrames.Add(keyFrame);
            this._sboardHide.Children.Add(animation);
            Storyboard.SetTarget(animation, this);
            Storyboard.SetTargetProperty(animation, new PropertyPath("(Canvas.Left)"));

            this._sboardShow.Begin();//启动动画
        }

        private void PopUP_Closed(object sender, EventArgs e)
        {
            Utils.Helper.Instance.StopAudio();
            if (this._closeTimer != null)
                this._closeTimer.Stop();
        }
        private void HideTimer_Tick(object sender, EventArgs e)
        {
            this._hideTimer.Stop();
            this._sboardHide.Begin();

            this._closeTimer.Start();
        }
        private void CloseTimer_Tick(object sender, EventArgs e)
        {
            this._closeTimer.Stop();
            this.Close();
        }
        private void PopWin_Loaded(object sender, RoutedEventArgs e)
        {
            this.Msg = this.Msg == null ? "" : this.Msg;
            this.Subject = this.Subject == null ? "" : this.Subject;
            this.Title = this.PopTitle == null ? "系统信息" : this.PopTitle;

            if (this.Subject.Length == 0)
            {
                this.txtInfo.Inlines.Remove(this.txtSubject);
            }
            else
            {
                this.txtSubject.Text = this.Subject.Length > 18 ? this.Subject.Substring(0, 18) + "..." : this.Subject;
                this.txtSubject.Text += "\r\n";
            }
            this.txtContent.Text = (this.Msg.Length > 58 ? this.Msg.Substring(0, 58) + "..." : this.Msg);
        }
    }
}
